//
//  TGCustomAddTabBar.m
//

#import "TGCustomAddTabBar.h"

@interface TGCustomAddTabBar ()

@property (nonatomic, weak) UIButton *addBtn;

@end

@implementation TGCustomAddTabBar

@dynamic delegate;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundImage = [UIImage imageNamed:@"tabbar_bg"];
        self.shadowImage = [[UIImage alloc]init];
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        addBtn.adjustsImageWhenHighlighted = NO;
        [addBtn setImage:[UIImage imageNamed:@"tabbar_add"] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(inquiryBtnClicked) forControlEvents:UIControlEventTouchDown];
        [self addSubview:addBtn];
        self.addBtn = addBtn;
        
    }
    return self;
}

- (void)inquiryBtnClicked
{
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickAddBtn:)]) {
        [self.delegate tabBarDidClickAddBtn:self];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat tabbarButtonW = self.width / AddTabItemCount;
    CGFloat margin = RatioWidth(18);
    // 1.设置加号按钮的位置
    self.addBtn.frame = CGRectMake(tabbarButtonW * 2+margin, RatioWidth(5), tabbarButtonW-margin*2, tabbarButtonW-margin*2);
    
    // 2.设置其他tabbarButton的位置和尺寸
    
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 设置宽度
            child.width = tabbarButtonW;
            // 设置x
            child.x = tabbarButtonIndex * tabbarButtonW;
            // 增加索引
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 2) {
                tabbarButtonIndex++;
            }
        }
    }
}

@end
